package com.example.learn.controller;

import com.example.learn.pojo.FrontKnow;
import com.example.learn.pojo.SortEntity;
import com.example.learn.service.SortService;
import com.example.learn.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sort")
public class SortController {

    @Autowired
    SortService sortService;

    @RequestMapping("/list")
    public R<Map> getKnowList() {
        List<SortEntity> sortList = sortService.getSortList();
        Map dataMap = new HashMap();
        dataMap.put("data", sortList);
        dataMap.put("total", sortList.size());
        return R.success(dataMap,"success");
    }

    @PostMapping("/add")
    @ResponseBody
    public R addSort(@RequestBody SortEntity sort) {
        int count = sortService.addSort(sort);
        if(count >= 1){
            return R.success("添加成功");
        }else {
            return R.failed("添加失败");
        }
    }

    @PostMapping("/update")
    public R updateSort(@RequestBody SortEntity sort) {
        int count = sortService.updateSort(sort);
        if(count >= 1){
            return R.success("修改成功");
        }else {
            return R.failed("修改失败");
        }
    }

    @GetMapping("/delete")
    public R delSortById(int id) {
        int count = sortService.delSortById(id);
        if(count >= 1){
            return R.success("删除成功");
        }else {
            return R.failed("删除失败");
        }
    }
}
